#include "SQLParserFeature.h"
#include "../../utils/log.h"
#include "../../utils/BOOL.h"

SQLParserFeature SQLParserFeature::KeepInsertValueClauseOriginalString = SQLParserFeature(1 << 0, make_string_ptr("KeepInsertValueClauseOriginalString"));
SQLParserFeature SQLParserFeature::KeepSelectListOriginalString = SQLParserFeature(1 << 1, make_string_ptr("KeepSelectListOriginalString"));
SQLParserFeature SQLParserFeature::UseInsertColumnsCache = SQLParserFeature(1 << 2, make_string_ptr("UseInsertColumnsCache"));
SQLParserFeature SQLParserFeature::EnableSQLBinaryOpExprGroup = SQLParserFeature(1 << 3, make_string_ptr("EnableSQLBinaryOpExprGroup"));
SQLParserFeature SQLParserFeature::OptimizedForParameterized = SQLParserFeature(1 << 4, make_string_ptr("OptimizedForParameterized"));
SQLParserFeature SQLParserFeature::OptimizedForForParameterizedSkipValue = SQLParserFeature(1 << 5, make_string_ptr("OptimizedForForParameterizedSkipValue"));
SQLParserFeature SQLParserFeature::KeepComments = SQLParserFeature(1 << 6, make_string_ptr("KeepComments"));
SQLParserFeature SQLParserFeature::SkipComments = SQLParserFeature(1 << 7, make_string_ptr("SkipComments"));
SQLParserFeature SQLParserFeature::StrictForWall = SQLParserFeature(1 << 8, make_string_ptr("StrictForWall"));
SQLParserFeature SQLParserFeature::TDDLHint = SQLParserFeature(1 << 9, make_string_ptr("TDDLHint"));
SQLParserFeature SQLParserFeature::DRDSAsyncDDL = SQLParserFeature(1 << 10, make_string_ptr("DRDSAsyncDDL"));
SQLParserFeature SQLParserFeature::DRDSBaseline = SQLParserFeature(1 << 11, make_string_ptr("DRDSBaseline"));
SQLParserFeature SQLParserFeature::InsertReader = SQLParserFeature(1 << 12, make_string_ptr("InsertReader"));
SQLParserFeature SQLParserFeature::IgnoreNameQuotes = SQLParserFeature(1 << 13, make_string_ptr("IgnoreNameQuotes"));
SQLParserFeature SQLParserFeature::KeepNameQuotes = SQLParserFeature(1 << 14, make_string_ptr("KeepNameQuotes"));
SQLParserFeature SQLParserFeature::SelectItemGenerateAlias = SQLParserFeature(1 << 15, make_string_ptr("SelectItemGenerateAlias"));
SQLParserFeature SQLParserFeature::PipesAsConcat = SQLParserFeature(1 << 16, make_string_ptr("PipesAsConcat"));
SQLParserFeature SQLParserFeature::InsertValueCheckType = SQLParserFeature(1 << 17, make_string_ptr("InsertValueCheckType"));
SQLParserFeature SQLParserFeature::InsertValueNative = SQLParserFeature(1 << 18, make_string_ptr("InsertValueNative"));
SQLParserFeature SQLParserFeature::EnableCurrentTimeExpr = SQLParserFeature(1 << 19, make_string_ptr("EnableCurrentTimeExpr"));
SQLParserFeature SQLParserFeature::EnableCurrentUserExpr = SQLParserFeature(1 << 20, make_string_ptr("EnableCurrentUserExpr"));
SQLParserFeature SQLParserFeature::KeepSourceLocation = SQLParserFeature(1 << 21, make_string_ptr("KeepSourceLocation"));
SQLParserFeature SQLParserFeature::SupportUnicodeCodePoint = SQLParserFeature(1 << 22, make_string_ptr("SupportUnicodeCodePoint"));
SQLParserFeature SQLParserFeature::PrintSQLWhileParsingFailed = SQLParserFeature(1 << 23, make_string_ptr("PrintSQLWhileParsingFailed"));
SQLParserFeature SQLParserFeature::EnableMultiUnion = SQLParserFeature(1 << 24, make_string_ptr("EnableMultiUnion"));
SQLParserFeature SQLParserFeature::Spark = SQLParserFeature(1 << 25, make_string_ptr("Spark"));
SQLParserFeature SQLParserFeature::Presto = SQLParserFeature(1 << 26, make_string_ptr("Presto"));
SQLParserFeature SQLParserFeature::MySQLSupportStandardComment = SQLParserFeature(1 << 27, make_string_ptr("MySQLSupportStandardComment"));

SQLParserFeature::SQLParserFeature() {}
SQLParserFeature::SQLParserFeature(SQLParserFeature *sqlParserFeature)
{
  this->mask = sqlParserFeature->mask;
  this->name = sqlParserFeature->name;
}

SQLParserFeature::SQLParserFeature(int mask, string_ptr name)
{
  this->mask = mask;
  this->name = name;
}

bool SQLParserFeature::operator==(SQLParserFeature &rgt)
{
  return name == rgt.name;
}

int SQLParserFeature::value()
{
  return (int)(mask);
}
string_ptr SQLParserFeature::toString()
{
  return (this->name);
}

BOOL_ptr SQLParserFeature::isEnabled(int features, SQLParserFeature_ptr feature)
{
  LOG_INFO << "features:" << features << ",feature->mask:" << feature->mask << ",features & feature->mask:" << (features & feature->mask);
  if ((features & feature->mask) != 0){
    return BOOL::TRUE;
  }else{
    return BOOL::FALSE;
  }
}

u_int64_t SQLParserFeature::config(int features, SQLParserFeature_ptr feature, BOOL_ptr state)
{
  if (state)
  {
    features |= feature->mask;
  }
  else
  {
    features &= ~feature->mask;
  }

  return features;
}

u_int64_t SQLParserFeature::of(SQLParserFeature_list_ptr features)
{
  if (features->empty())
  {
    return 0;
  }
  u_int64_t v = 0;
  for (auto p1 = features->begin(); p1 != features->end(); p1++)
  {
    v |= (*p1)->value();
  }
  return v;
}
